﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Windows.Forms;

namespace WinFormsGraphicsDevice
{
    public class BasicGraphicsDeviceControl : GraphicsDeviceControl
    {
        Stopwatch timer;
        Texture2D backDropTexture;

        // Vertex positions and colors used to display a spinning triangle.
        public readonly VertexPositionColor[] Vertices =
        {
            new VertexPositionColor(new Vector3(-1, -1, 0), Color.Black),
            new VertexPositionColor(new Vector3( 1, -1, 0), Color.Black),
            new VertexPositionColor(new Vector3( 0,  1, 0), Color.Black),
        };



        int cols = 32;
        protected override void Initialize()
        {
            
            // Start the animation timer.
            timer = Stopwatch.StartNew();

            Texture2D tex = new Texture2D(this.GraphicsDevice, cols, cols);
            int alpha = 50;
            var colora = Color.FromNonPremultiplied(255, 255, 2555, alpha);
            var colorb = Color.FromNonPremultiplied(0, 0, 0, 125);

            List<Color> colors = new List<Color>();
            var col = colora;
            for (int b = 0; b < 2; b++)
            {


                for (int i = 0; i < cols; i++)
                {
                    for (int x = 0; x < cols / 2; x++)
                    {
                        colors.Add(col);
                    }

                    if (col == colora)
                        col = colorb;
                    else
                        col = colora;
                }
                col = colorb;
            }

            tex.SetData<Color>(colors.ToArray());
            if (DefaultTexture == null)
            {
                DefaultTexture = new Texture2D(this.GraphicsDevice, 1, 1);
                DefaultTexture.SetData<Color>(new Color[] { Color.White });
            }
            backDropTexture = tex;
            internalBatch = new SpriteBatch(this.GraphicsDevice);
            // Hook the idle event to constantly redraw our animation.
            Application.Idle += delegate { Invalidate(); };
        }
        protected static Texture2D DefaultTexture;
        SpriteBatch internalBatch = null;
        protected override void Draw()
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            internalBatch.Begin();

            var width = GraphicsDevice.Viewport.Width / cols + 1;
            var height = GraphicsDevice.Viewport.Height / cols + 1;
            for (int i = 0; i < width; i++)
            {
                for (int x = 0; x < height; x++)
                {
                    internalBatch.Draw(this.backDropTexture, new Rectangle(i * cols, x * cols, cols, cols), Color.White);
                }
            }
            
            
            
            internalBatch.End();
            // Spin the triangle according to how much time has passed.
            float time = (float)timer.Elapsed.TotalSeconds;

        }

    }
}
